package controllers.api;

import java.util.ArrayList;
import java.util.List;

import org.bson.types.ObjectId;
import org.eclipse.jdt.core.dom.ThisExpression;

import models.User;
import models.Group;
public class Users extends API {
	
	public static void create(){
		
		validation.required("name",request.params.get("name")).message("Invalid Name");
		validation.minSize("name",request.params.get("name"), 3).message("Invalid Name");
		validation.required("email",request.params.get("email")).message("Invalid Email");
		validation.email("email",request.params.get("email")).message("Invalid Email");
		validation.required("password",request.params.get("password")).message("Invalid Password");
        validation.minSize("password",request.params.get("password"), 6).message("Invalid Password");
        if(validation.hasErrors()){
        	json(validation.errors());
        	//json(validation.errors());
        }else{
        	User user = new User();
       	    user.setName(request.params.get("name"));
    		user.setEmail(request.params.get("email"));
    		user.setPassword(request.params.get("password"));
    		user.setMonth(request.params.get("month"));
    		user.setDay(request.params.get("day"));
    		user.setYear(request.params.get("year"));
    		user.setGroup(request.params.get("group"));
    		user.setPrPhoto("default_avatar.png");
    	    Group value=Group.find("byName",request.params.get("group")).first();
    	    Group.addMember(value, user);
    		json(user);
        }
		
	}
	

	
	public static void list(){
		List<User> users = User.find().fetch();
		json(users);
	}
}
